package com.zyt.service.Impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zyt.mapper.ClazzMapper;
import com.zyt.pojo.Clazz;
import com.zyt.pojo.ClazzQueryParam;
import com.zyt.pojo.PageResult;
import com.zyt.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Clazz clazz) {
        //补全基础属性 - update_time
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Clazz clazz) {
        //补全基础属性 - create_time,update_time
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        //再插入新数据
        clazzMapper.insert(clazz);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Clazz> findAll() {
        return clazzMapper.findAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Clazz getInfo(Integer id) {
        //给status赋值
        return clazzMapper.getById(id);
    }

    @Autowired
    private ClazzMapper clazzMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageResult<Clazz> page(ClazzQueryParam clazzqueryParam) {
        PageHelper.startPage(clazzqueryParam.getPage(), clazzqueryParam.getPageSize());
        List<Clazz> rows = clazzMapper.rows(clazzqueryParam);
        Page<Clazz> p = (Page<Clazz>) rows;
        return new PageResult<Clazz>(p.getTotal(), p.getResult());

    }
}
